package com.fmjava.service_video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fmjava.service_video.entity.Chapter;
import com.fmjava.service_video.entity.Video;
import com.fmjava.service_video.entity.vo.ChapterVO;
import com.fmjava.service_video.entity.vo.ContentVideoVO;
import com.fmjava.service_video.mapper.ChapterMapper;
import com.fmjava.service_video.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fmjava.service_video.service.VideoService;
import org.apache.velocity.runtime.directive.contrib.For;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 作品章节 服务实现类
 * </p>
 *
 * @author fmjava
 * @since 2021-07-19
 */
@Service
@Transactional
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    VideoService videoService;

    @Override
    public  List<ChapterVO> getChapterContentVideo(String contentId) {
        List<ChapterVO> chapterVOList = new ArrayList<>();

        //1、查询所有章节
        QueryWrapper<Chapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("content_id",contentId);
        List<Chapter> chapterList=baseMapper.selectList(wrapperChapter);

        //2、查询章节里的所有小节
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("content_id",contentId);
        List<Video> list = videoService.list(wrapperVideo);
        
        //封装每个章节里面的对应小节
        for (Chapter chapter : chapterList) {

            ChapterVO chapterVo = new ChapterVO();
            BeanUtils.copyProperties(chapter, chapterVo);

            List<ContentVideoVO> contentVideoVOList = new ArrayList<>();
            for (Video video : list) {


                if (video.getChapterId().equals(chapter.getId())){
                    ContentVideoVO contentVideoVO = new ContentVideoVO();
                    BeanUtils.copyProperties(video, contentVideoVO);
                    contentVideoVOList.add(contentVideoVO);

                }
            }
            chapterVo.setContentVideoVOList(contentVideoVOList);
            chapterVOList.add(chapterVo);
        }
        return chapterVOList;
    }

    @Override
    public void deleteChapter(String id) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content_id",id);
        baseMapper.delete(queryWrapper);
    }

}
